﻿#!/usr/bin/python
# -*- coding: utf-8  -*-

import os
import sys
import urllib
import re, datetime,time
import wikipedia, config,catlib
site = wikipedia.getSite()

template='Translating'
#tpage=u'User:Sz-iwbot/翻译'
#得到使用该模板的条目
s=wikipedia.Page(site, 'Template:%s' % template)
#print s.editTime()
pages = [p for p in s.getReferences() if p.namespace()==0]
#print pages

#p=wikipedia.Page(site, u'中国')
#print p.editTime()
def formattime(timestamp):
 #   """
 #   格式化当前时间
  #  """
    t=time.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
    y=time.strftime("%Y",time.gmtime())
    m=time.strftime("%m",time.gmtime())
    d=time.strftime("%d",time.gmtime())
    h=time.strftime("%H",time.gmtime())
    min=time.strftime("%M",time.gmtime())
    s=time.strftime("%S",time.gmtime())
    nowtime=datetime.datetime(int(y),int(m),int(d),int(h),int(min),int(s))
	
def GetPage(rcstart,rcend, rcns):
    #"""从最近更改API获取XML正文"""
    url2unicode
    baseurl = 'http://zh.wikipedia.org/w/api.php?action=query&list=recentchanges&rcstart=%s&rcend=%s&rcnamespace=%s&rcprop=title|user|timestamp|comment&rclimit=500&format=xml'

    uo=wikipedia.MyURLopener()
    url = baseurl % (rcstart, rcend, rcns)
    u = uo.open(url)
    wikipedia.output(u'URL:\n%s' % url)
    return u.read()
	
    p = xml.parsers.expat.ParserCreate()  
    p.StartElementHandler = start_element  
    p.returns_unicode = True 
    p.Parse(html) 

for page in pages:
#    page=wikipedia.Page(site, title)
#    text=page.get()
#    set=lset=True
    for (id, timestamp, user, content) in page.getVersionHistory():
#        wikipedia.output(u'time:%s     user:%s' % (edittime, user))
      
        if page.latestRevision() == int(id):
            last=timestamp
            latesttime=formattime(timestamp)
            print formattime(timestamp)
#            text=content
#             lset=False
#                time=timestamp
            
            if datetime.datetime.utcnow()- formattime(timestamp) > datetime.timedelta(days=90):
#            set=False
#    title=page.title()
#    page=wikipedia.Page(site.title)
#    print page.editTime()
#    if edittime(page) <> None:

#    if datetime.datetime.utcnow()-laset> datetime.timedelta(days=90):
                text=page.get()
                tt=u'{{过期翻译|time=%s}}\n' %  page.editTime()
                text=tt+text
                page.put(text, u'Bot更新：超过3个月未有翻译，标记清理')        
                wikipedia.output(u'[[%s]] have 90 days Not Translation: Latest edit time is %s' % (page.title(), last)         
			 
	
